﻿using System;
using System.Collections;
using System.Data;
using Model;
using Service;
using Service.ReferenceData;
using Resources;
using ArrayList=System.Collections.ArrayList;


public partial class OwnEdit : System.Web.UI.Page
{
    readonly ReferenceDataService _ReferenceDataService = new ReferenceDataService();
    readonly StockService _StockService = new StockService();
    private DirectService _DirectService = new DirectService();
    private readonly ReferenceDataServiceSupplier _ReferenceDataServiceSupplier = new ReferenceDataServiceSupplier();
    private readonly DirectServiceSupplier _DirectServiceSupplier = new DirectServiceSupplier();
    private readonly DirectServiceOwner _DirectServiceOwner = new DirectServiceOwner();
    private int codenumber = 0;

    protected void Page_Load(object sender, EventArgs e)
    {

     codenumber = GetCodeNumber();

     pnlBody.Visible = codenumber == 0 ? false : true;
     if (codenumber == 0) return;


     if (!IsPostBack)
     {

         if (codenumber > 0)
         {
              
             DataTable table = new DataTable();
             table = _DirectServiceOwner.GetOwnerForEdit(codenumber.ToString());
              if(table.Rows.Count >0)
              {

                  dlBuilding.SelectedValue = table.Rows[0]["BuildingId"].ToString();
                  lblSystem.Text = table.Rows[0]["System"].ToString();
                  lblDepartment.Text = table.Rows[0]["DepartmentName"].ToString();
                  lblModel.Text = table.Rows[0]["Model"].ToString();
                  lblTypeequipment.Text = table.Rows[0]["TypeEquipment"].ToString();
                 
                  dlLocation.SelectedValue = table.Rows[0]["LocationId"].ToString();
                  dlEmployee.SelectedValue = table.Rows[0]["EmployeeId"].ToString();
                  DataTable tableCodeNumber = new DataTable();
                  tableCodeNumber =
                      _DirectServiceOwner.GetCodeNumberByEmployeeId(int.Parse(table.Rows[0]["EmployeeId"].ToString()));
                 // lblCodeEquipment.Text = tableCodeNumber.Rows[0]["CODENUMBER"].ToString(); 
                  IList listCodeEquipment = new ArrayList();
                  for(int i=0;i<tableCodeNumber.Rows.Count ;i++)
                  {
                      listCodeEquipment.Add(tableCodeNumber.Rows[i]["CODENUMBER"].ToString());

                  }
                  if(listCodeEquipment.Count >0) // bind codeNumber by EmployeeId
                  {
                      dlCodeEquipment.DataSource = listCodeEquipment;
                      dlCodeEquipment.DataBind();
                  }
              }
         }
     }
    }

    private void RecallData ()
    {
        DataTable table = new DataTable();
        table = _DirectServiceOwner.GetOwnerForEdit(codenumber.ToString());
        if (table.Rows.Count > 0)
        {

            dlBuilding.SelectedValue = table.Rows[0]["BuildingId"].ToString();
            lblSystem.Text = table.Rows[0]["System"].ToString();
            lblDepartment.Text = table.Rows[0]["DepartmentName"].ToString();
            lblModel.Text = table.Rows[0]["Model"].ToString();
            lblTypeequipment.Text = table.Rows[0]["TypeEquipment"].ToString();

            dlLocation.SelectedValue = table.Rows[0]["LocationId"].ToString();
            dlEmployee.SelectedValue = table.Rows[0]["EmployeeId"].ToString();
            DataTable tableCodeNumber = new DataTable();
            tableCodeNumber =
                _DirectServiceOwner.GetCodeNumberByEmployeeId(int.Parse(table.Rows[0]["EmployeeId"].ToString()));
            // lblCodeEquipment.Text = tableCodeNumber.Rows[0]["CODENUMBER"].ToString(); 
            IList listCodeEquipment = new ArrayList();
            for (int i = 0; i < tableCodeNumber.Rows.Count; i++)
            {
                listCodeEquipment.Add(tableCodeNumber.Rows[i]["CODENUMBER"].ToString());

            }
            if (listCodeEquipment.Count > 0) // bind codeNumber by EmployeeId
            {
                dlCodeEquipment.DataSource = listCodeEquipment;
                dlCodeEquipment.DataBind();
            }
        }
        
    }

    protected void bntValidate_Click(object sender, EventArgs e)
    {
        if (!new Utility().TestPermissions(Resource.PermissionEditOwner))
        {
            lblPermission.Text = Resource.Permission;
            RecallData();
            return;
        }

        if (codenumber > 0)
        {
           // DateTime dateEnter = new DateTime();
            int employeeId = int.Parse(dlEmployee.SelectedItem.Value);
            int buildingId = int.Parse(dlBuilding.SelectedItem.Value);
            int locationId = int.Parse(dlLocation.SelectedItem.Value);

            string dateEnter = string.Empty;
            if (txtDateEnter.Text != "")
            {
                dateEnter = txtDateEnter.Text;
                 
            }
            else {
                lblPermission.Text = "Must be select DateEnter!";
                return;
                }

            _DirectServiceOwner.UpdateOwner(employeeId, buildingId, locationId, dateEnter, codenumber.ToString());
            lblPermission.Text = Resource.MessageValidate;
 
        }

    }
    public int GetCodeNumber()
    {
        return !String.IsNullOrEmpty(Request.QueryString["codenumber"]) ? Int32.Parse(Request.QueryString["codenumber"]) : 0;
    }

    protected void dsEmployee_Selecting(object sender, System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs e)
    {

    }
    protected void dsEmployee_Selecting1(object sender, System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs e)
    {

    }
}
